home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_05 / colton / ips.c < prev    next >
C/C++ Source or Header  |  1995-02-21  |  9KB  |  530 lines

  1. /*
  2.  * Listing 2 - ips.c
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <rpc/rpc.h>
  8. #include <sys/dir.h>
  9. #include <unistd.h>
  10. #include "ips.h"
  11.  
  12. /* Constants */
  13. #define STORAGE_DIR     getenv("STORAGE_DIR")
  14. #define LOADERS_DIR     getenv("LOADERS_DIR")
  15. #define SAVERS_DIR      getenv("SAVERS_DIR")
  16. #define OPS_DIR         getenv("OPS_DIR")
  17.  
  18. /* Prototypes */
  19. void            ips_hello();
  20. void            ips_decode();
  21. void            ips_process();
  22. void            ips_encode();
  23. void            ips_release();
  24. void            ips_request_list();
  25. void            ips_request_args();
  26. char            *replace_ext();
  27. char            *get_filename();
  28. char            *upcase();
  29. int                get_next_id();
  30. unsigned char    *loadfile();
  31.  
  32.  
  33. Packet *
  34. ips_1(request)
  35. Packet *request;
  36. {
  37.     static Packet    reply;
  38.     char            sbuf[1024];
  39.  
  40.     xdr_free(xdr_Packet, reply);
  41.  
  42.     switch(request->op) {
  43.         case IPS_INIT:
  44.             ips_hello(request, &reply);
  45.             break;
  46.  
  47.         case IPS_DECODE:
  48.             ips_decode(request, &reply);
  49.             break;
  50.  
  51.         case IPS_REQUEST_IMAGE:
  52.             ips_encode(request, &reply);
  53.             break;
  54.  
  55.         case IPS_PROCESS:
  56.             ips_process(request, &reply);
  57.             break;
  58.  
  59.         case IPS_IMAGE_RELEASE:
  60.             ips_release(request, &reply);
  61.             break;
  62.  
  63.         case IPS_REQUEST_OPS:
  64.             ips_request_list(request, &reply);
  65.             break;
  66.  
  67.         case IPS_REQUEST_LOADERS:
  68.             ips_request_list(request, &reply);
  69.             break;
  70.  
  71.          case IPS_REQUEST_SAVERS:
  72.             ips_request_list(request, &reply);
  73.             break;
  74.  
  75.         default:
  76.             reply.op = ERROR;
  77.     }
  78.  
  79.     return &reply;
  80. }
  81.  
  82.  
  83. void
  84. ips_release(request, reply)
  85. Packet *request, *reply;
  86. {
  87.     char buf[1024];
  88.  
  89.     sprintf(buf, "rm -f %s/%d:*",
  90.         STORAGE_DIR, request->Packet_u.id);
  91.  
  92.     system(buf);
  93.  
  94.     reply->op = IPS_RELEASE_OK;
  95. }
  96.  
  97.  
  98. void
  99. ips_hello(request, reply)
  100. Packet *request, *reply;
  101. {
  102.     reply->Packet_u.hello_string = "Welcome to IPS.";
  103.     reply->op = IPS_HELLO;
  104. }
  105.  
  106.  
  107. void
  108. ips_request_list(request, reply)
  109. Packet *request, *reply;
  110. {
  111.     int                i,
  112.                     found_listings = 0;
  113.     char            *temp_name;
  114.     list            *cp,
  115.                     *nlp = NULL;
  116.     static DIR        *dirp;
  117.     struct direct    *d;
  118.  
  119.  
  120.     printf("IPS_REQUEST received.\n");
  121.  
  122.     switch(request->op) {
  123.         case IPS_REQUEST_OPS:
  124.             dirp = opendir(OPS_DIR);
  125.             break;
  126.  
  127.         case IPS_REQUEST_LOADERS:
  128.             dirp = opendir(LOADERS_DIR);
  129.             break;
  130.  
  131.         case IPS_REQUEST_SAVERS:
  132.             dirp = opendir(SAVERS_DIR);
  133.             break;
  134.     }
  135.  
  136.     nlp = &reply->Packet_u.operators;
  137.  
  138.     while(d = readdir(dirp)) {
  139.         temp_name = d->d_name;
  140.  
  141.         if(strcmp(temp_name, ".") == 0 ||
  142.                 strcmp(temp_name, "..") == 0)
  143.             continue;
  144.  
  145.         found_listings = 1;
  146.         nlp->name = strdup(temp_name);
  147.         cp = nlp;
  148.         nlp->next = (list *) malloc(sizeof(list));
  149.         nlp = nlp->next;
  150.     }
  151.  
  152.     closedir(dirp);
  153.  
  154.     if(found_listings)
  155.         cp->next = nlp = NULL;
  156.     else {
  157.         nlp->name = "NONE_AVAILABLE";
  158.         nlp->next = NULL;
  159.     }
  160.  
  161.     switch(request->op) {
  162.         case IPS_REQUEST_OPS:
  163.             reply->op = IPS_SEND_OPS;
  164.             break;
  165.  
  166.         case IPS_REQUEST_LOADERS:
  167.             reply->op = IPS_SEND_LOADERS;
  168.             break;
  169.  
  170.         case IPS_REQUEST_SAVERS:
  171.             reply->op = IPS_SEND_SAVERS;
  172.             break;
  173.     }
  174. }
  175.  
  176.  
  177. void
  178. ips_process(request, reply)
  179. Packet *request, *reply;
  180. {
  181.     int i, stat;
  182.     long len;
  183.     char buf[2000];
  184.     char buf2[2020];
  185.     unsigned char *data;
  186.     char *filename = NULL;
  187.  
  188.  
  189.     printf("IPS_PROCESS received.\n");
  190.  
  191.     sprintf(buf, "%s/%s", OPS_DIR,
  192.         request->Packet_u.proc.command);
  193.  
  194.     filename =
  195.         get_filename(request->Packet_u.send_ops.id);
  196.  
  197.     if(access(buf, R_OK) != 0 ||
  198.             strlen(filename) == 0) {
  199.         reply->op = ERROR;
  200.         return;
  201.     }
  202.  
  203.     sprintf(buf, "%s/%s %s/%s %s/%s",
  204.         OPS_DIR,
  205.         request->Packet_u.proc.command,
  206.         STORAGE_DIR,
  207.         filename,
  208.         STORAGE_DIR,
  209.         filename);
  210.  
  211.     if(request->Packet_u.proc.argc > 0) {
  212.         list *lp;
  213.  
  214.         lp = &request->Packet_u.proc.argv;
  215.  
  216.         for(i=0;i<request->Packet_u.proc.argc &&
  217.                 lp != NULL;i++, lp=lp->next) {
  218.  
  219.             strcat(buf, " ");
  220.             strcat(buf, lp->name);
  221.         }
  222.     }
  223.  
  224.     if(system(buf) != 0)
  225.         reply->op = ERROR;
  226.     else
  227.         reply->op = IPS_PROCESS_OK;
  228.  
  229.     return;
  230. }
  231.  
  232.  
  233. void
  234. ips_encode(request, reply)
  235. Packet *request, *reply;
  236. {
  237.     int i, stat;
  238.     long len;
  239.     char buf[2000];
  240.     char buf2[2020];
  241.     unsigned char *data;
  242.     char *filename = NULL;
  243.  
  244.  
  245.     printf("IPS_REQUEST_IMAGE received.\n");
  246.  
  247.     filename =
  248.         get_filename(request->Packet_u.send_ops.id);
  249.  
  250.     if(strlen(filename) == 0)
  251.         reply->op = IPS_IMAGE_NOT_AVAIL;
  252.     else  {
  253.         printf("\tSending %s image.\n",
  254.             request->Packet_u.send_ops.format);
  255.  
  256.             sprintf(buf, "%s/%s", SAVERS_DIR,
  257.                 request->Packet_u.send_ops.format);
  258.  
  259.             if(access(buf, R_OK) != 0) {
  260.                 reply->op = IPS_UNSUPPORTED_FORMAT;
  261.                 return;
  262.             }
  263.  
  264.             sprintf(buf, "%s/%s %s/%s %s/%s",
  265.                 SAVERS_DIR,
  266.                 request->Packet_u.send_ops.format,
  267.                 STORAGE_DIR,
  268.                 filename,
  269.                 STORAGE_DIR,
  270.                 replace_ext(filename,
  271.                 request->Packet_u.send_ops.format));
  272.  
  273.             if(request->Packet_u.send_ops.argc > 0) {
  274.                 list *lp;
  275.  
  276.                 lp = &request->Packet_u.send_ops.argv;
  277.  
  278.                 for(i=0;
  279.                     i<request->Packet_u.send_ops.argc
  280.                     && lp != NULL;i++, lp=lp->next) {
  281.  
  282.                     strcat(buf, " ");
  283.                     strcat(buf, lp->name);
  284.                 }
  285.             }
  286.  
  287.             printf("\tConverting IPS to %s...\n",
  288.                 request->Packet_u.send_ops.format);
  289.  
  290.             stat = system(buf);
  291.  
  292.             if(stat != 0) {
  293.                 printf("\tError %d.\n", stat);
  294.                 reply->op = ERROR;
  295.  
  296.                 sprintf(buf, "%s/%s",
  297.                 STORAGE_DIR, replace_ext(filename,
  298.                 request->Packet_u.send_ops.format));
  299.  
  300.                 sprintf(buf2, "rm -f %s", buf);
  301.                 system(buf2);
  302.  
  303.                 return;
  304.             }
  305.  
  306.             printf("\tSuccess.\n");
  307.  
  308.             sprintf(buf, "%s/%s", STORAGE_DIR,
  309.                 replace_ext(filename,
  310.                 request->Packet_u.send_ops.format));
  311.  
  312.             data = loadfile(buf, &len);
  313.  
  314.             sprintf(buf2, "rm -f %s", buf);
  315.             system(buf2);
  316.  
  317.             reply->Packet_u.img.data.data_len = len;
  318.             reply->Packet_u.img.data.data_val =
  319.                 (char *) data;
  320.             reply->op = IPS_SEND_IMAGE;
  321.     }
  322. }
  323.  
  324.  
  325. void
  326. ips_decode(request, reply)
  327. Packet *request, *reply;
  328. {
  329.     FILE *fp;
  330.     char buf[256];
  331.     char format[10];
  332.     unsigned char *data;
  333.     unsigned long id;
  334.     int status;
  335.     char new_filename[1024];
  336.  
  337.  
  338.     printf("IPS_DECODE received.\n");
  339.  
  340.     id = get_next_id();
  341.  
  342.     reply->Packet_u.id = id;
  343.  
  344.     sprintf(buf, "%s/%ld:%s",
  345.         STORAGE_DIR, id,
  346.         request->Packet_u.raw_img.filename);
  347.  
  348.     if((fp = fopen(buf, "w")) == NULL) {
  349.         printf("\tError in opening file %s.\n", buf);
  350.         reply->op = ERROR;
  351.         return;
  352.     }
  353.  
  354.     fwrite(request->Packet_u.raw_img.data.data_val, 1, 
  355.         request->Packet_u.raw_img.data.data_len, fp);
  356.     fclose(fp);
  357.  
  358.     strcpy(new_filename, buf);
  359.     strcpy(format,
  360.         upcase(request->Packet_u.raw_img.format));
  361.  
  362.     if(strcmp(format, "UNKNOWN") == 0) {
  363.         printf("\tUnknown format.\n");
  364.         reply->op = IPS_UNKNOWN_FORMAT;
  365.         return;
  366.     }
  367.  
  368.     sprintf(buf, "%s/%s", LOADERS_DIR, format);
  369.  
  370.     if(access(buf, R_OK) == 0) {
  371.         sprintf(buf, "%s/%s %s %s",
  372.             LOADERS_DIR, format, new_filename,
  373.                 replace_ext(new_filename, "ips"));
  374.  
  375.         printf("\tConverting: DATA...%s...", format);
  376.         fflush(stdout);
  377.  
  378.         status = system(buf);
  379.  
  380.         if(status == 0) {
  381.             printf("\n\tSuccess.\n");
  382.             sprintf(buf, "rm %s", new_filename);
  383.             system(buf);
  384.             reply->op = IPS_OK_ID;
  385.  
  386.         } else {
  387.             printf("\tUnsupported format.\n");
  388.             reply->op = IPS_UNSUPPORTED_FORMAT;
  389.         }
  390.  
  391.     } else {
  392.         printf("\tUnsupported format.\n");
  393.         reply->op = IPS_UNSUPPORTED_FORMAT;
  394.     }
  395. }
  396.  
  397.  
  398. int
  399. get_next_id()
  400. {
  401.     static unsigned long current_id = 0;
  402.  
  403.     current_id++;
  404.  
  405.     if(current_id > 999999)
  406.         current_id = 0;
  407.  
  408.     return current_id;
  409. }
  410.  
  411.  
  412. char *
  413. replace_ext(filename, ext)
  414. char *filename;
  415. char *ext;
  416. {
  417.     static char newbuf[1024];
  418.     char temp[1024];
  419.     char *p;
  420.  
  421.     strcpy(temp, filename);
  422.     p = temp;
  423.  
  424.     while(*p != '.' && *p != 0)
  425.         p++;
  426.  
  427.     if(*p != 0)
  428.         *p = 0;
  429.  
  430.     strcpy(newbuf, temp);
  431.     strcat(newbuf, ".");
  432.     strcat(newbuf, ext);
  433.  
  434.     return newbuf;
  435. }
  436.  
  437.  
  438. char *
  439. get_filename(id)
  440. unsigned long id;
  441. {
  442.     static DIR *dirp;
  443.     struct direct *d;
  444.     int temp_id;
  445.     static char filename[1024];
  446.     struct filelist_node {
  447.         char *filename;
  448.         struct filelist_node *next;
  449.     };
  450.  
  451.     struct filelist_node filelist, *clp, *flp;
  452.  
  453.     dirp = opendir(STORAGE_DIR);
  454.  
  455.     flp = &filelist;
  456.  
  457.     while(d = readdir(dirp)) {
  458.         flp->filename = d->d_name;
  459.         flp->next = (struct filelist_node *)
  460.             malloc(sizeof(struct filelist_node));
  461.         clp = flp;
  462.         flp = flp->next;
  463.     }
  464.  
  465.     closedir(dirp);
  466.  
  467.     clp->next = NULL;
  468.     filename[0] = NULL;
  469.  
  470.     for(flp=&filelist;flp != NULL; flp=flp->next) {
  471.         int temp_id;
  472.  
  473.         if(strcmp(flp->filename, ".") == 0 ||
  474.             strcmp(flp->filename, "..") == 0)
  475.                 continue;
  476.  
  477.         sscanf(flp->filename, "%d:", &temp_id);
  478.  
  479.         if(temp_id == id)
  480.             strcpy(filename, flp->filename);
  481.     }
  482.  
  483.     for(flp=&filelist;